package com.chf.controller;

import com.chf.bean.UserInfoBean;
import com.chf.service.impl.UserService;
import com.chf.utils.MyUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class AjaxUserControler {
    @Resource
    UserService userService;

//    用户信息查询
    @RequestMapping("/AjaxUserInfoQuery")
    public ResponseEntity<List<UserInfoBean>> AjaxUserInfoQuery(UserInfoBean userInfoBean){
        List<UserInfoBean> list = userService.selectUserInfo(userInfoBean);
        return new ResponseEntity(list,HttpStatus.OK);
    }
    @RequestMapping("/AjaxUserInfoQueryById")
    public ResponseEntity<UserInfoBean> AjaxUserInfoQueryById(Integer userid){
        return new ResponseEntity(userService.selectByPrimaryKey(userid),HttpStatus.OK);
    }

//    用户信息添加
//    添加用户
    @ResponseBody
    @RequestMapping("/AjaxUserInfoAdd")
    public String UserInfoAdd(UserInfoBean userInfoBean,@RequestParam("file_userimg") MultipartFile multipartFile){
        int i=0;
        ////////////////文件处理操作////////////////
        MyUtils.doSingleFileUpload(multipartFile,"uploadfiles","userimg",userInfoBean,"userimg");
        ////////////////插入数据操作////////////////
        //若userid不为null，则进行修改操作，否则进行添加
        if(userInfoBean.getUserid()!=null){
            i = userService.updateByPrimaryKeySelective(userInfoBean);
        }else {
            i = userService.insertSelective(userInfoBean);
        }
        if (i > 0) {
            return "true";
        } else {
            return "false";
        }
    }
    //    删除用户
    @ResponseBody
    @RequestMapping("/AjaxUserInfoDel")
    public String UserInfoDel(Integer userid){
        System.out.println("userid---"+userid);
        int i = userService.deleteByPrimaryKey(userid);
        if(i>0){
            return "true";
        }else {
            return "false";
        }
    }


    //异步请求 ，不经过视图解析@ResponseBody
    @RequestMapping("/Ajaxtest")
    public String AjaxTest(String name, String age){
        System.out.println("name--"+name+"\n age--"+age);
        return "name--"+name+", age--"+age;
    }
    //使用ResponseEntity可以代替@ResponseBody，且功能更强大
    @RequestMapping("/Ajaxtest2")
    public ResponseEntity<String> AjaxTest2(String name, String address){
        System.out.println("name--"+name+"\n address--"+address);
        UserInfoBean userInfoBean = userService.selectByPrimaryKey(70);
        return new ResponseEntity(userInfoBean, HttpStatus.OK);
//        return ResponseEntity.ok("name--"+name+"\n address--"+address);
    }
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}
